package com.crk.yiyuanguahao.controller;

import com.crk.yiyuanguahao.entity.Role;
import com.crk.yiyuanguahao.entity.RoleAuth;
import com.crk.yiyuanguahao.entity.RoleAuthDto;
import com.crk.yiyuanguahao.util.LayUiTableJson;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseResult;
import com.crk.yiyuanguahao.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author
 * @description: 角色管理
 * @date 2020/1/29
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @RequestMapping("manage")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView("/workspace/role/roleList.html");
        return modelAndView;
    }

    @GetMapping("detail")
    public ModelAndView detail(){
        ModelAndView modelAndView = new ModelAndView("/workspace/role/roleAdd.html");
        return modelAndView;
    }

    @GetMapping("permission")
    public ModelAndView permission(){
        ModelAndView modelAndView = new ModelAndView("/workspace/role/permission.html");
        return modelAndView;
    }

    /**
     * 保存角色
     * @param role 角色
     * @return
     */
    @RequestMapping("/saveRole")
    public ResponseResult saveRole(Role role){
        ResponseResult ResponseResult = roleService.saveRole(role);
        return ResponseResult;
    }



    /**
     * 删除角色
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(@RequestParam int roleId){
        ResponseResult ResponseResult = roleService.deleteRoleById(roleId);
        return ResponseResult;
    }

    /**
     * 查询角色列表
     * @param roleCode 角色编码
     * @param roleName 角色名称
     * @param page 当前页数
     * @param limit 每页数据量
     * @return
     */
    @RequestMapping("/queryRoleList")
    public LayUiTableJson queryRoleList(String roleCode, String roleName
            , @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit){
        Page pageInfo = new Page();
        pageInfo.setCurrentPage(page);
        pageInfo.setSize(limit);
        ResponseResult ResponseResult = roleService.getRoleList(roleCode,roleName,pageInfo);
        LayUiTableJson layUiTableJson = new LayUiTableJson(0,null,pageInfo.getAllRow(),(List)ResponseResult.getData());
        return layUiTableJson;
    }
    @RequestMapping("/queryRoleById")
    public ResponseResult queryRoleById(int roleId){
        ResponseResult ResponseResult = roleService.getRole(roleId);
        return ResponseResult;
    }
    /**
     * 修改角色状态
     * @param roleId 角色id
     * @param status 状态
     * @return
     */
    @RequestMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestParam int roleId, @RequestParam int status){
        ResponseResult ResponseResult = roleService.changeStatus(roleId,status);
        return ResponseResult;
    }
    /**
     * 停用角色
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/suspendRole")
    public ResponseResult suspendRole(@RequestParam int roleId){
        ResponseResult ResponseResult = roleService.changeStatus(roleId,2);
        return ResponseResult;
    }

    /**
     * 启用角色
     * @param roleId 角色id
     * @return
     */
    @RequestMapping("/resumeRole")
    public ResponseResult resumeRole(@RequestParam int roleId){
        ResponseResult ResponseResult = roleService.changeStatus(roleId,1);
        return ResponseResult;
    }

    @RequestMapping("/batchUpdateRoleAuth")
    public ResponseResult batchUpdateRoleAuth(@RequestBody RoleAuthDto roleAuthDto){
        ResponseResult ResponseResult = roleService.batchUpdateRoleAuth(roleAuthDto);
        return ResponseResult;
    }
}
